﻿using FairyGUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class PackEquipPage : BasePage
{
    private GList lsItems;
    private GTextField tfCount;

    public PackEquipPage(GComponent vie) : base(vie)
    {
        lsItems = this.view.GetChild("n6").asList;
        tfCount = this.view.GetChild("n7").asTextField;

        lsItems.onClickItem.Add(OnPackItemClick);
    }

    public override void Show()
    {
        this.Load();
    }

    public override void Close()
    {
        
    }

    private void Load()
    {
        lsItems.RemoveChildrenToPool();
        List<Equip> equips = GameNetwork.Player.PackEquipMgr.GetAll();
        equips.Sort(EquipComparison);
        foreach (Equip equip in equips)
        {
            InitEquip initEquip = equip.InitEquip;
            if (initEquip != null)
            {
                GComponent button = lsItems.GetFromPool("ui://f2iijc3nm9n1bl").asCom;
                button.data = equip.id;

                GTextField tfName = button.GetChild("n1").asTextField;
                tfName.text = initEquip.name;
                tfName.color = CommonUtil.GetEquipColor(initEquip.quality);

                GTextField tfQiangHua = button.GetChild("n2").asTextField;
                tfQiangHua.SetVar("lv", equip.QiangHuaLevel.ToString()).FlushVars();

                GTextField tfJingLian = button.GetChild("n3").asTextField;
                tfJingLian.SetVar("lv", equip.JingLianLevel.ToString()).FlushVars();

                int value = equip.Attribute.Get(initEquip.GetAttributeType());
                GTextField tfValue = button.GetChild("n4").asTextField;
                tfValue.SetVar("name", CommonUtil.GetEquipPosName(initEquip.kind)).SetVar("value", value.ToString()).FlushVars();

                for (int j = 1; j <= 4; j++)
                {
                    GImage cpt = button.GetChild("bg" + j.ToString()).asImage;
                    if (cpt != null)
                    {
                        cpt.visible = j == initEquip.quality;
                    }
                }

                lsItems.AddChild(button);
            }
        }
        tfCount.text = string.Format("{0}/{1}", equips.Count, 999);
    }

    private int EquipComparison(Equip a, Equip b)
    {
        int pa = 0;
        if (a.InitEquip != null)
        {
            pa = a.InitEquip.quality;
        }
        int pb = 0;
        if (b.InitEquip != null)
        {
            pb = b.InitEquip.quality;
        }
        if (pa > pb)
        {
            return -1;
        }
        else if (pa < pb)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }

    private void OnPackItemClick(EventContext context)
    {
        GButton button = lsItems.GetChildAt(lsItems.selectedIndex).asButton;
        if (button == null)
        {
            return;
        }
        long id = (long)button.data;

        if (GameNetwork.Player.PackEquipMgr.TryGet(id, out Equip equip))
        {
            PopupPanelManager.Open<EquipInfoPanel>(equip);
        }
    }
}
